//
//  WBPushTrasion.swift
//  Swift_sina
//
//  Created by TFB_WGP on 2017/8/17.
//  Copyright © 2017年 TFB_WGP. All rights reserved.
//

import UIKit

class WBTransitioningDelegate: NSObject, UIViewControllerTransitioningDelegate{
    
    
    
}

class WBAnimationTransitioning: NSObject,UIViewControllerAnimatedTransitioning {
    
    var isPresent: Bool?
    
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.35
    }
    
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        
        //1.获取动画的源控制器和目标控制器
        let fromViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)!
        let toViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)!
        let containerView = transitionContext.containerView
        
        let finalFrameForVC = transitionContext.finalFrame(for: toViewController)
        if isPresent! {
            
            toViewController.view.frame = CGRect(x: -SCREEN_WIDTH, y: 0, width: finalFrameForVC.width, height: finalFrameForVC.height)
            containerView.addSubview(toViewController.view)
            UIView.animate(withDuration: 0.35, animations: {
                
                fromViewController.view.alpha = 0.8
                toViewController.view.frame = finalFrameForVC;
                
            }) { (finished) in
                transitionContext.completeTransition(true)
                fromViewController.view.alpha = 1.0
            }
            
        }else{
            
            
            toViewController.view.frame = CGRect(x: SCREEN_WIDTH, y: 0, width: finalFrameForVC.width, height: finalFrameForVC.height)
            containerView.addSubview(toViewController.view)
            UIView.animate(withDuration: 0.35, animations: {
                
                fromViewController.view.alpha = 0.8
                toViewController.view.frame = finalFrameForVC;
                
            }) { (finished) in
                
                    transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
                    fromViewController.view.alpha = 1.0
            }
        }
        
    }
}
